package com.general.run.task;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class ForkAndJoinTest {

    public static void main(String[] args) {


        //4个线程
        ForkJoinPool fjp = new ForkJoinPool(4);

        Fibonacci fib = new Fibonacci(30);

        Integer invoke = fjp.invoke(fib);

        System.out.println(invoke);

    }


    static class Fibonacci extends RecursiveTask<Integer>{


        final int n;

        Fibonacci(int n) {
            this.n = n;
        }

        @Override
        protected Integer compute() {
            if (n <= 1){
                return n;
            }
            Fibonacci f1 = new Fibonacci(n - 1);
            f1.fork();
            Fibonacci f2 = new Fibonacci(n - 2);

            return f2.compute() + f1.join();
        }
    }
}
